﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hospital.Models
{
    public class DocReposite
    {
        private HostleDataContext _datacontext;

        public DocReposite(HostleDataContext datacontext)
        {
            _datacontext = datacontext;
        }

        public IEnumerable<Doctor> GetDoctors()
        {
            return _datacontext.Doctors.OrderBy(d => d.FIO);
        }

        public IEnumerable<string> GetDoctorsSpec()
        {
            return (from i in _datacontext.Doctors select i.Spec).Distinct();
        }

        public Doctor GetDoctorsBySpec(string spec)
        {
            return _datacontext.Doctors.FirstOrDefault(d => d.Spec == spec);
        }

        public Doctor GetDoctor(int id)
        {
            return _datacontext.Doctors.SingleOrDefault(d => d.Id_Doc == id);
        }

        public Doctor AddDoctor(string fio, string spec, string uchastok, string kab)
        {
            Doctor d = new Doctor { FIO = fio, Spec = spec, Uchastok = uchastok, Kab = kab };
            _datacontext.Doctors.InsertOnSubmit(d);
            _datacontext.SubmitChanges();
            return d;
        }

        public void UpdateDoctor(Doctor d)
        {
            Doctor dbDoc = GetDoctor(d.Id_Doc);
            dbDoc.FIO = d.FIO;
            dbDoc.Spec = d.Spec;
            dbDoc.Uchastok = d.Uchastok;
            dbDoc.Kab = d.Kab;
            dbDoc.AssignedOn = d.AssignedOn;
            _datacontext.SubmitChanges();
        }

        public void DeleteDoc(int id)
        {
            Doctor dbDoc = GetDoctor(id);
            _datacontext.Doctors.DeleteOnSubmit(dbDoc);
            _datacontext.SubmitChanges();
        }
    }
}